﻿using Generic.App.Domain.Entity;
using Generic.App.Domain.IRepository;
using Generic.Helper.Sql;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using System.Dynamic;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;

namespace Generic.App.EntityFrameworkCore.Repository
{
    public class AccountRepository : EfCoreRepository<GenericAppContext, App_Account, string>, IAccountRepository
    {
        private IDbContextProvider<GenericAppContext> _dbContext;
        private IConfiguration _configuration;
        public AccountRepository(IDbContextProvider<GenericAppContext> dbContext, IConfiguration configuration) : base(dbContext)
        {
            _dbContext = dbContext;
            _configuration = configuration;
        }

        public List<dynamic> GetActiveAccount_Demo()
        {
            string connectionString = _configuration.GetConnectionString("GenericAppConnectionStr");
            var commandText = "SELECT AppId,AppSecret FROM dbo.App_Account WHERE IsActive=1";
            Func<SqlDataReader, List<dynamic>> dataReaderDelegate = executeDataReaderDelegate;
            return SqlHelper.GetDataReader(connectionString, commandText, new List<SqlParameter>(), dataReaderDelegate);
        }

        private List<dynamic> executeDataReaderDelegate(SqlDataReader dataReader)
        {
            List<dynamic> results = new List<dynamic>();

            while (dataReader.Read())
            {
                dynamic dy = new ExpandoObject();
                dy.AppId = dataReader[0];
                dy.AppSecret = dataReader[1];
                results.Add(dy);
            }
            return results;
        }
    }
}
